package com.syyo.gulimall.product.app;

//import org.springframework.security.access.prepost.PreAuthorize;

import com.syyo.common.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.syyo.gulimall.product.domain.req.AttrGroupReq;
import com.syyo.gulimall.product.service.AttrGroupService;
import com.syyo.common.domain.ResultVo;


/**
 * 属性分组
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:32:12
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 新增
     */
    @PostMapping
    // @PreAuthorize("@syyo.check('system:attrgroup:add')")
    public ResultVo add(@RequestBody AttrGroupReq req) {
        return ResultUtils.ok(attrGroupService.add(req));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    // @PreAuthorize("@syyo.check('system:attrgroup:del')")
    public ResultVo del(@PathVariable("id") Integer id) {
        return ResultUtils.ok(attrGroupService.del(id));
    }

    /**
     * 修改
     */
    @PutMapping
    // @PreAuthorize("@syyo.check('system:attrgroup:edit')")
    public ResultVo edit(@RequestBody AttrGroupReq req) {
        return ResultUtils.ok(attrGroupService.edit(req));
    }

    /**
     * 信息
     */
    @GetMapping("/{id}")
    public ResultVo findOne(@PathVariable("id") Integer id) {
        return ResultUtils.ok(attrGroupService.findOne(id));
    }

    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}/{pageNum}/{pageSize}")
    public ResultVo findPage(@PathVariable("catelogId") Long catelogId,
                             @PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             AttrGroupReq req) {
        req.setAttrGroupId(catelogId);
        return ResultUtils.ok(attrGroupService.findPage(pageNum, pageSize, req));
    }

    /**
     * 根据分类查询列表
     */
    @GetMapping("/list/{catelogId}")
    public ResultVo findAll(@PathVariable("catelogId") Long catelogId) {
        return ResultUtils.ok(attrGroupService.findAll(catelogId));
    }

    /**
     * 列表，根据分组查询列表
     */
    @GetMapping("/group/list/{groupId}")
    public ResultVo findAllByGroupId(@PathVariable("groupId") Long groupId) {
        return ResultUtils.ok(attrGroupService.findAllByGroupId(groupId));
    }

    /**
     * 删除关联
     */
    @DeleteMapping("/{attrId}/{groupId}")
    // @PreAuthorize("@syyo.check('system:attrgroup:del')")
    public ResultVo delRelevance(@PathVariable("attrId") Integer attrId, @PathVariable("groupId") Integer groupId) {
        return ResultUtils.ok(attrGroupService.delRelevance(attrId, groupId));
    }


    /**
     * 查询当前分类没有关联的属性
     */
    @GetMapping("/noattr/relevance/list/{groupId}/{pageNum}/{pageSize}")
    public ResultVo findAllNoRelevance(@PathVariable("groupId") Long groupId,
                                       @PathVariable("pageNum") Integer pageNum,
                                       @PathVariable("pageSize") Integer pageSize) {
        return ResultUtils.ok(attrGroupService.findAllNoRelevance(groupId, pageNum, pageSize));
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @GetMapping("/group/list/withattr/{catelogId}")
    public ResultVo findAttrAndGroupByCatelogId(@PathVariable("catelogId") Long catelogId) {
        return ResultUtils.ok(attrGroupService.findAttrAndGroupByCatelogId(catelogId));
    }

}
